package dao;

import domain.User;
import util.DbUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    DbUtil dbu=new DbUtil();
    public int pageSize=5;

//    通过用户名，密码，角色号匹配进行登录网络
    public User findUser(int userid, String password, int role) {
        String sql="select * from user where user_id='"+userid+"'and user_psw='"+password+"' and role='"+role+"'";
        System.out.println(sql);
        ResultSet rs=dbu.executeQuery(sql);//结果集rs
        try {
            if (rs.next()) {
                return converToUser(rs);
            }else {
                return null;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

    }

//    通过用户名对用户进行查询
    public List<User> findAllUser(String name,int current){
        String sql="select * from user where user_name='"+name+"'";
        System.out.println(sql);
        ResultSet rs=dbu.executeQuery(sql);
        List<User> users=new ArrayList<User>();
        try {
            while (rs.next()){
                User u=converToUser(rs);
                users.add(u);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return users;
    }

//    通过用户ID对用户内容进行查询
    public User findUserById(int id) {
        String sql="select * from user where user_id='"+id+"'";
        System.out.println(sql);
        ResultSet rs=dbu.executeQuery(sql);
        try {
            if (rs.next()){
                return converToUser(rs);
            }else {
                return null;
            }
        }catch (SQLException e){
            e.printStackTrace();
            return null;
        }
    }

//    通过用户名遍历列表
public List<User>   findUsersByName(String name,int current){
    String sql="select * from user where user_name like '%"+name+"%' order by user_id limit "+(current-1)*pageSize+","+pageSize;
    System.out.println(sql);
    ResultSet rs=dbu.executeQuery(sql);
    List<User> users=new ArrayList<User>();
    try {
        while(rs.next()) {
            User u=converToUser(rs);
            users.add(u);
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return users;
}


//    通过id删除用户信息
public boolean deleteUserById(int id) {
        String sql="delete from user where user_id='"+id+"'";
        System.out.println(sql);
        if(dbu.executeUpdate(sql)>0){
            return true;
        }else {
            return false;
        }
}

//    重置密码
    public boolean reset_psw(int id) {
        String sql="update user set user_psw='123456' where user_id='"+id+"'";
        System.out.println(sql);
        if(dbu.executeUpdate(sql)>0){
            return true;
        }else {
            return false;
        }
    }

//    修改用户信息
    public boolean EditUser(User user){
        String sql = "update user set user_name='"+user.getUsername()+"', sex="+user.getSex()+" , phone='"+user.getPhone()+"', address='"+user.getAddress()+"' where user_id="+user.getUserid()+" ";
        System.out.println(sql);
        if(dbu.executeUpdate(sql)>0){
            return true;
        }else {
            return false;
        }
    }

//    获取页码
public int getTotal(String name) {
    String sql="select count(1) as total from user where user_name like '%"+name+"%'";
    ResultSet rs=dbu.executeQuery(sql);
    int total=0;
    try {
        if (rs.next()) {
            total=rs.getInt("total");
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return total;
}




    public User converToUser(ResultSet rs)  {
        User u = null;
        try {
            String userid=rs.getString(1);
            String username=rs.getString(2);
            String password=rs.getString(3);
            int sex=rs.getInt(4);
            String phone=rs.getString(5);
            String address=rs.getString(6);
            String createTime=rs.getString(7);
            int role=rs.getInt(8);
            u=new User(userid,username,password,sex,phone,address,createTime,role);
            return u;

        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
}
